using System;
using System.Collections;
using System.Xml;

using LocalBoard.Domain;
using LocalBoard.Persistence;

namespace LocalBoard.Controller
{
	/// <summary>
	/// Summary description for ClubController.
	/// </summary>
	public class ClubController
	{
		private int _clubId = 0;
		private IAccessorFactory _accessorFactory;
		private IBoardAccessor _boardAccessor;
		private IClubAccessor _clubAccessor;
		private IClubProfileAccessor _clubProfileAccessor;
		private IClubRoleAccessor _clubRoleAccessor;

		public int ClubId
		{
			get {return _clubId;}
		}

		public ClubController(int clubId, IAccessorFactory accessorFactory)
		{
			_clubId = clubId;
			if(accessorFactory == null)
				throw new ArgumentException("Accessor Factory shouldn't be null", "accessorFactory");
			_accessorFactory = accessorFactory;
			_boardAccessor = _accessorFactory.GetBoardAccessor();
			_clubAccessor = _accessorFactory.GetClubAccessor();
			_clubProfileAccessor = _accessorFactory.GetClubProfileAccessor();
			_clubRoleAccessor = _accessorFactory.GetClubRoleAccessor();
		}

		public ArrayList GetBoardList()
		{
			return _boardAccessor.GetBoardListByClubId(_clubId);
		}

		public Board GetBoard(int boardId)
		{
			return _boardAccessor.Get(boardId);
		}

		public void UpdateBoard(Board board)
		{
			_boardAccessor.Update(board);
		}

		public void DeleteBoard(int boardId)
		{
			_boardAccessor.Delete(boardId);
		}

		public int CreateBoard(Board board)
		{
			return _boardAccessor.Insert(board);
		}

		public ClubSecurity GetClubSecurity()
		{
			return new ClubSecurity(this, _accessorFactory);
		}

		public Hashtable GetAllUsersInClub()
		{
			return _clubProfileAccessor.GetAllUserClubProfiles(_clubId);
		}
		public Hashtable GetAllClubRoles()
		{
			return _clubRoleAccessor.GetAllClubRoles(_clubId);
		}

		public void DeleteUserRole(int id)
		{
			if(_clubRoleAccessor.Get(id, _clubId).Name != UserRole.CLUB_MANAGER_ROLE_NAME)
				_clubRoleAccessor.Delete(id, _clubId);
			else
				throw new Exception("Manager role can not be deleted.");
		}

		public int CreateClubRole(string roleName)
		{
			return _clubRoleAccessor.Insert(roleName, _clubId);
		}

		public void AddUserClubProfile(XmlNode clubProfile, int profileId)
		{
			_clubProfileAccessor.Insert(profileId, clubProfile, _clubId);
		}

		public bool IsUserInTheClub(int profileId)
		{
			return _clubProfileAccessor.IsUserInTheClub(profileId, _clubId);
		}

		public XmlNode GetUserClubProfile(int profileId)
		{
			return _clubProfileAccessor.Get(profileId, _clubId);
		}

		public void UpdateUserClubProfile(XmlNode clubProfile, int profileId)
		{
			_clubProfileAccessor.Update(profileId, clubProfile, _clubId);
		}

		public void AssignClubRoleToUser(int clubRoleId, int profileId)
		{
			_clubRoleAccessor.AddClubRoleToProfile(clubRoleId, profileId, _clubId);
		}

		public bool IsUserInClubRole(int profileId, int clubRoleId)
		{
			foreach(UserRole role in _clubRoleAccessor.GetClubRolesByProfileId(profileId, _clubId))
			{
				if(role.Id == clubRoleId)
					return true;
			}
			return false;
		}

		public bool IsUserInClubRole(int profileId, string clubRoleName)
		{
			foreach(UserRole role in _clubRoleAccessor.GetClubRolesByProfileId(profileId, _clubId))
			{
				if(role.Name == clubRoleName)
					return true;
			}
			return false;
		}

		public void AssignClubManager(int profileId)
		{
			if(!IsUserInTheClub(profileId))
				AddUserClubProfile(null, profileId);
			_clubAccessor.AssignManagerToClub(profileId, _clubId);
		}

		public Profile GetClubManager()
		{
			UserRole managerRole = _clubRoleAccessor.Get(UserRole.CLUB_MANAGER_ROLE_NAME, _clubId);
			if(managerRole == null)
				return null;
			foreach(int userId in GetAllUsersInClub().Keys)
			{
				if(this.IsUserInClubRole(userId, managerRole.Id))
					return _accessorFactory.GetProfileAccessor().Get(userId);
			}
			return null;
		}

		public ArrayList GetClubRolesAssignedToUser(int profileId)
		{
			return _accessorFactory.GetClubRoleAccessor().GetClubRolesByProfileId(profileId, _clubId);
		}

		public void UnAssignClubRoleFromUser(int clubRoleId, int profileId)
		{
			_accessorFactory.GetClubRoleAccessor().DeleteClubRoleFromProfile(clubRoleId, profileId, _clubId);
		}

		public bool UserIsClubManager(Profile user)
		{
			return IsUserInClubRole(user.Id, UserRole.CLUB_MANAGER_ROLE_NAME);
		}
	}
}
